﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class PublicCommunication_ComGroupManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.DataList_DataTable();
        }
    }

    protected void DataList_DataTable()
    {
        PublicCommunicationGroupService pcgService = new PublicCommunicationGroupService();
        List<PublicCommunicationGrouptb> list = pcgService.findALL();
        if (list != null && list.Count > 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ComGroupId", typeof(string));
            dt.Columns.Add("ComGroupName", typeof(string));
            foreach (PublicCommunicationGrouptb pcg in list)
            {
                DataRow dr = dt.NewRow();
                dr["ComGroupId"] = pcg.PcgId.ToString();
                dr["ComGroupName"] = pcg.PcgName;
                dt.Rows.Add(dr);
            }
            this.DataList1.DataSource = dt.DefaultView;
        }
        else
        {
            this.DataList1.DataSource = null;
        }
        this.DataList1.DataBind();
    }

    protected void BtnCreateGroup_Click(object sender, ImageClickEventArgs e)
    {
       
    }

    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        string groupName = Request.Form.Get(this.TxtComGroupName.ClientID);
        if (!"".Equals(groupName.Trim()))
        {
            PublicCommunicationGroupService pcgService = new PublicCommunicationGroupService();
            if (pcgService.saveComGroupInfo(groupName) == 1)
            {
                this.DataList_DataTable();
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.parent.leftFrame.location.reload();", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('创建分组失败，请重试！');", true);
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string pcg_id = ((Button)sender).Attributes["pcg_id"];
        Response.Redirect("EditComGroupInfo.aspx?pcg_id=" + pcg_id);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int pcg_id = int.Parse(((Button)sender).Attributes["pcg_id"]);
        PublicCommunicationGroupService pcgService = new PublicCommunicationGroupService();
        if (pcgService.delById(pcg_id) == 1)
        {
            this.DataList_DataTable();
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.parent.leftFrame.location.reload();", true);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (((Button)sender).Attributes["pcg_id"] != null)
        {
            string cgName = ((Button)sender).Attributes["pcg_name"];
            int cgid = int.Parse(((Button)sender).Attributes["pcg_id"]);
            PublicCommunicationGroupLinkManService pcgLmService = new PublicCommunicationGroupLinkManService();
            List<PublicCommunicationGroupLinkMantb> list = pcgLmService.findByComGroupId(cgid);
            if (list == null)
            {
                list = new List<PublicCommunicationGroupLinkMantb>();
            }
            StringBuilder sbu = new StringBuilder();
            sbu.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
            sbu.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
            sbu.AppendLine("<td>姓名</td>");
            sbu.AppendLine("<td>性别</td>");
            sbu.AppendLine("<td>生日</td>");
            sbu.AppendLine("<td>昵称</td>");
            sbu.AppendLine("<td>职务</td>");
            sbu.AppendLine("<td>单位名称</td>");
            sbu.AppendLine("<td>单位地址</td>");
            sbu.AppendLine("<td>单位电话</td>");
            sbu.AppendLine("<td>单位传真</td>");
            sbu.AppendLine("<td>家庭住址</td>");
            sbu.AppendLine("<td>家庭电话</td>");
            sbu.AppendLine("<td>手机号码</td>");
            sbu.AppendLine("<td>电子邮件</td>");
            sbu.AppendLine("<td>OICQ号码</td>");
            sbu.AppendLine("<td>手机小号</td>");
            sbu.AppendLine("<td>备注</td>");
            sbu.AppendLine("</tr>");
            foreach (PublicCommunicationGroupLinkMantb ucgLinkMan in list)
            {
                sbu.AppendLine("<tr>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManName + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + (ucgLinkMan.GroupLinkManSec == 0 ? "男" : "女") + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManBirthday.ToShortDateString() + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManName2 + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManDuty + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitName + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitAddress + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitTelephone + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManUnitFacsimile + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHomeAddress + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHomeTelephone + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManHandset + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManEmail + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManOicq + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManMsn + "</td>");
                sbu.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + ucgLinkMan.GroupLinkManRemark + "</td>");
                sbu.AppendLine("</tr>");
            }
            sbu.AppendLine("</table>");
            Page.Response.Clear();
            Page.Response.Buffer = true;
            Page.Response.Charset = "GB2312";
            Page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(cgName, Encoding.UTF8) + ".xls");
            Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            Page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            Page.EnableViewState = false;
            Page.Response.Write(sbu.ToString());
            Page.Response.End();
            //}
        }
    }

    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }
}
